package com.java.base.exercise.current;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author RenPu
 * @Date 2021/8/12 10:24
 * @Version 1.0
 * @Description:
 **/
public class MyData {
    private volatile int j = 0;

//    private final static AtomicInteger j=new AtomicInteger(0);




    /**
     * 对J进行加操作
     */
    public synchronized void add() {
        j++;
//        j.incrementAndGet();
        System.out.println("加操作的线程" + Thread.currentThread().getName() + "j 为：" + j);
    }


    /**
     * 对J进行减操作
     */
    public synchronized void dec() {
//        j.decrementAndGet();
        j--;
        System.out.println("减线程的操作" + Thread.currentThread().getName() + "j 为：" + j);
    }


    /**
     * 获取操作后的J值
     *
     * @return
     */
    public int getData() {
        return j;
    }

    public static void main(String[] args) {
        MyData data = new MyData();
        Runnable add = new AddRunnable(data);
        Runnable dec = new DecRunnable(data);
        for (int i = 0; i < 2; i++) {
            new Thread(add).start();
            new Thread(dec).start();
        }

//        for (int i = 0; i <3 ; i++) {
//            new Thread(()->{
//            j.incrementAndGet();
//                System.out.println("累加的结果为"+j.get());
//            j.decrementAndGet();
//                System.out.println("递减的结果"+j.get());
//            }).start();
//
//        }

    }
}
